﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.Entity.Validation;
using HYI.SDBSERVER;
using eHCBaseClass;
using Sunny.UI;

namespace SHRSYSTEM
{
    public partial class FMGROUP : SDBSERVER.FMSINGLE
    {
        //入口函数
        public static Int32 StartMe(string sMainPara, string sPara, string sUser, DBSERVER sDB)
        {
            StaticClass.Selectm_MainPara = sMainPara;
            StaticClass.SelectItemValue = sPara;
            StaticClass.SelectUser = sUser;
            StaticClass.myDB = sDB;
            return 0;
        }

        private string sType = "V";
        //DBSERVER myADO = new DBSERVER();
        DBCache myCache = new DBCache();
        private int iGridView = 0;

        public FMGROUP()
        {
            InitializeComponent();
        }

        //数据保存成功后执行操作
        private void doProc()
        {
            string sql = " update TABLEVERSION set VERSION=VERSION+1 where TABLENAME='AM_SYS_ROLE'";
            StaticClass.myDB.doexec(sql);

            myCache.doCache();
        }

        private void FMGROUP_Load(object sender, EventArgs e)
        {
            sUser = StaticClass.SelectUser;
            string[] strArray = StaticClass.Selectm_MainPara.Split(new char[] { ';' });
            sMenu = strArray[1];

            sys05.Visible = false; sys05.Tag = "1";
            sys06.Visible = false; sys06.Tag = "1";
            sys07.Visible = false; sys07.Tag = "1";
            sys10.Visible = false; sys10.Tag = "1";
            sys11.Text = "授权"; sys11.Visible = true;

            OnSys04();
        }

        //删除代码
        protected override void OnSys03()
        {
            Console.WriteLine("行:" + rowindex.ToString());
            if (rowindex == -1)
            {
                return;
            }

            if (this.ShowAskDialog("确定删除(" + uiDGView["name", rowindex].Value.ToString() + ")?"))
            {
                string sBillNo = uiDGView["id", rowindex].Value.ToString();
                string SQL = "exec proc_biz_dodelete 'am_sys_role','','" + sBillNo + "',2,'" + sUser + "'";
                DataTable myTable = StaticClass.myDB.query(SQL);

                string sMsg = myTable.Rows[0][0].ToString();
                if (sMsg.Equals("OK"))
                {
                    this.ShowInfoDialog("删除完毕");
                    myCache.doCache();
                    OnSys04();
                }
                else
                {
                    this.ShowInfoDialog(sMsg);
                }
            }
        }

        //查询代码
        protected override void OnSys04()
        {
            string SQL = "select * from am_sys_role where 1=1";
            Qrytable = StaticClass.myDB.query(SQL);

            base.OnSys04();

            //表格列显示统一控制
            if (iGridView == 0)
            {
                myCache.doSetGridVIew(sMenu, sUser, uiDGView);
                iGridView = iGridView + 1;
            }

            rowindex = -1;
        }

        //新增代码
        protected override void OnSys01()
        {
            uitxtfGroupId.Text = "";
            uitxtfGroupName.Text = "";

            sType = "A";
            uitxtfGroupId.ReadOnly = false;

            base.OnSys01();
        }

        //编辑代码
        protected override void OnSys02()
        {
            Console.WriteLine("行:" + rowindex.ToString());
            if (rowindex == -1)
            {
                return;
            }
            //string s = uiDGView.Rows[rowindex].Cells[0].Value.ToString();
            uitxtfGroupId.Text = uiDGView["id", rowindex].Value.ToString();
            uitxtfGroupName.Text = uiDGView["name", rowindex].Value.ToString();
            sType = "B";
            uitxtfGroupId.ReadOnly = true;

            base.OnSys02();
        }

        //授权
        protected override void OnSys11()
        {
            Console.WriteLine("行:" + rowindex.ToString());
            if (rowindex == -1)
            {
                return;
            }
            string sgroupid = uiDGView.Rows[rowindex].Cells[0].Value.ToString();
            string sgroupname = uiDGView.Rows[rowindex].Cells[1].Value.ToString();

            FMGROUPMENU mygroupmenu = new FMGROUPMENU();
            mygroupmenu.sgroupid = sgroupid;
            mygroupmenu.sgroupname = sgroupname;
            mygroupmenu.myADO = StaticClass.myDB;
            mygroupmenu.ShowDialog();
            mygroupmenu.Dispose();

        }

        private bool CheckData()
        {
            return CheckEmpty(uitxtfGroupId, "请输入角色编号")
                   && CheckEmpty(uitxtfGroupName, "请输入角色名称");
        }

        //保存代码
        protected override void OnSys08()
        {
            if (!CheckData())
            {
                return;
            }

            if (sType == "A")
            {
                try
                {
                    using (XTBasEntities context = new XTBasEntities())
                    {
                        am_sys_role myData = context.am_sys_role.Find(int.Parse(uitxtfGroupId.Text));
                        if (myData is null)
                        {
                            am_sys_role myData2 = new am_sys_role();
                            myData2.id = int.Parse(uitxtfGroupId.Text);
                            myData2.name = uitxtfGroupName.Text;
                            myData2.description = uidescription.Text;
                            myData2.create_id = int.Parse(sUser);
                            myData2.create_time = DateTime.Now;
                            myData2.status = 1;
                            myData2.sort_seq = 1;
                            myData2.update_id= int.Parse(sUser);
                            myData2.update_time = DateTime.Now;
                            context.am_sys_role.Add(myData2);
                            context.SaveChanges();

                            doProc();
                            UIMessageTip.ShowOk("添加成功");
                            OnSys04();

                        }
                        else
                        {
                            //Console.WriteLine("添加成功");
                            UIMessageTip.ShowOk("编号已经存在");
                            //Console.WriteLine("已存在");
                            return;
                        }
                        
                    }
                }
                catch (DbEntityValidationException dbEx)
                {
                    Console.WriteLine("错误" + dbEx.Message);
                }
            }

            else if (sType == "B")
            {
                using (XTBasEntities context = new XTBasEntities())
                {
                    //查询
                    am_sys_role mySYS_GROUP = context.am_sys_role.Find(int.Parse(uitxtfGroupId.Text));

                    mySYS_GROUP.name = uitxtfGroupName.Text;
                    mySYS_GROUP.description = uidescription.Text;
                    mySYS_GROUP.update_id = int.Parse(sUser);
                    mySYS_GROUP.update_time = DateTime.Now;
                    int result = context.SaveChanges();

                    doProc();
                    UIMessageTip.ShowOk("修改成功");
                    OnSys04();
                }
            }

            //刷新cache

            sType = "V";
            base.OnSys08();
        }

    }
}
